package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.FixedAssetsAddDTO;
import com.oa.admin.dto.FixedAssetsConditionDTO;
import com.oa.admin.entity.FixedAssets;
import com.oa.admin.service.IFixedAssetsService;
import com.oa.admin.vo.FixedAssetsUpdateDTO;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/fixedAssets")
@Api(description = "固定资产表模块")
//@ApiIgnore
public class FixedAssetsController extends BaseController {

    private final IFixedAssetsService fixedAssetsService;

    @Autowired
    public FixedAssetsController(IFixedAssetsService fixedAssetsService) {
        this.fixedAssetsService = fixedAssetsService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @PostMapping("/findList")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp findList(FixedAssetsConditionDTO fixedAssetsConditionDTO) throws DataIsEmptyException {
        return new Resp().success(fixedAssetsService.findFixedAssetsListCondition(fixedAssetsConditionDTO));
    }

//    @GetMapping("/export")
//    @CommonController
//    @Description("导出")
//    @ApiOperation(value = "导出")
//    public void export(FixedAssetsConditionDTO fixedAssetsConditionDTO) throws DataIsEmptyException {
//    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @PostMapping("/findById")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<FixedAssets> findById(@ApiParam(value = "id", required = true) @RequestParam String id) {
        return new Resp().success(fixedAssetsService.findFixedAssetsById(id));
    }


    /**
     * 新增
     *
     * @param fixedAssetsAddDTO FixedAssetsAddDTO
     * @return Resp
     */
    @PostMapping("/add")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "固定资产表对象") @RequestBody(required = false) FixedAssetsAddDTO fixedAssetsAddDTO) throws BusinessException {
        return fixedAssetsService.addFixedAssets(fixedAssetsAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @param fixedAssetsUpdateVO FixedAssetsUpdateDTO
     * @return Resp
     */
    @PutMapping("/update")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "固定资产表对象") @RequestBody(required = false) FixedAssetsUpdateDTO fixedAssetsUpdateVO) throws BusinessException {
        return fixedAssetsService.updateFixedAssetsById(fixedAssetsUpdateVO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        FixedAssets fixedAssets = fixedAssetsService.selectById(id);
        if (fixedAssets != null && fixedAssets.getIsDeleted() == 2) {
            fixedAssets.setIsDeleted(1);
            if (fixedAssetsService.updateById(fixedAssets)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
